'''
@author:gxj 2018/12
'''

class myPaginator(object):
    ''' 自定义分页'''
    def __init__(self,baseURL,totalCount,currentPage,pageZize=10,pageNum=11):
        self.base_url = baseURL
        self.total_count =totalCount
        # 数据总数
        try:
            self.current_page = int(currentPage)
        except Exception as e:
            self.current_page =1
        if self.current_page <= 0:
            self.current_page =1
        # 当前页数
        self.every_page_item_num = pageZize
        # 每页显示条数
        self.page_num = pageNum
        # 每页展示最大页码数
    '''baseURL,总数，当前页码数为必传参数'''
    @property
    def num_pages(self):
        '''计算总页数'''
        # 666/10
        a,b =divmod(int(self.total_count),int(self.every_page_item_num))
        if b ==0:
            return a
        return a+1

    def start(self):

        return (self.current_page - 1)* self.every_page_item_num

    def end(self):

        return self.current_page * self.every_page_item_num

    def  pre_page_range(self):
        '''页码数'''
        # current_page  # 当前页码数
        # per_pager_num #  当前页最多显示页码数量 11
        # num_pages # 页码总数
        if self.num_pages < self.page_num:
            return range(1,self.num_pages+1)
        else:
            part = int(self.page_num/2)
            # if self.current_page <= part:
            # 总页数大于当前页显示的页码数
            if (self.current_page+part) < self.page_num:
                return range(1,self.page_num+1)

            if (self.current_page + part) > self.num_pages:
                # 特殊处理大于总页数的时候
                return range(self.num_pages - self.page_num +1,self.num_pages+1)
            # 一般情况前后各去一半
            return range(self.current_page - part,self.current_page + part+1)

    def page_str(self):
        pagelist = []
        frist = "<li><a class='page' href='%s?p=1'>首页</a></li>" %(self.base_url)
        pagelist.append(frist)

        if self.current_page == 1:
            prev = "<li><a class='page' href='%s?p=%s'><span class='glyphicon glyphicon-menu-left' aria-hidden='true'></span></li>" %(self.base_url,1)
        else:
            prev = "<li><a class='page' href='%s?p=%s'><span class='glyphicon glyphicon-menu-left' aria-hidden='true'></span></a></li>" % (self.base_url,self.current_page -1)
        pagelist.append(prev)

        for i in self.pre_page_range():
            if i== self.current_page:
                temp = "<li class='page' class='active'><a href='%s?p=%s'>%s</a></li>" % (self.base_url,i,i)
            else:
                temp = "<li><a class='page' href='%s?p=%s'>%s</a></li>" % (self.base_url,i,i)
            pagelist.append(temp)

        if self.current_page == self.num_pages:
            next = "<li><a class='page' href='%s?p=%s'><span class='glyphicon glyphicon-menu-right' aria-hidden='true'></span></a></li>" % (self.base_url,self.num_pages)
        else:
            next = "<li><a class='page' href='%s?p=%s'><span class='glyphicon glyphicon-menu-right' aria-hidden='true'></span></a></li>" % (self.base_url,self.current_page +1,)
        pagelist.append(next)
        last = "<li><a class='page' href='%s?p=%s'>尾页</a></li>" %(self.base_url,self.num_pages,)
        pagelist.append(last)
        # print(pagelist)
        return ''.join(pagelist)
